﻿
namespace Sidvall.WorkItemManager.Data.ConfigModel.EntityManagers
{
    public partial class ServerConnectionManager<TEntity, TList>
    {
        #region Public Members

        #region CreateServerConnection

        public TEntity CreateServerConnection(IMonitorContext value)
        {
            if (value == null)
                return default(TEntity);
            var item = (TEntity)this.EntityFacadeManager.ResourceFactory.CreateItem(Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.ServerConnection, null);
            item.ConnectionTypeId = value.ConnectionTypeId;
            item.ConnectionString = value.ConnectionString;
            item.RemoteAddress = value.RemoteAddress;
            if (!IsValid(item))
                return default(TEntity);
            return item;
        }

        #endregion
        #region InitializeNewValues

        public void InitializeNewValues(IServerConnection value, Sidvall.WorkItemManager.Data.ConnectionType connectionType)
        {
            if (value == null)
                return;
            value.ConnectionTypeId = connectionType;
            value.Name = "** New Server **";
            value.IsEnabled = true;
        }

        #endregion
        #region IsValid

        public bool IsValid(IServerConnection value)
        {
            if (value == null)
                return false;
            switch (value.ConnectionTypeId)
            {
                case ConnectionType.RemoteAccess:
                    if (string.IsNullOrWhiteSpace(value.RemoteAddress))
                        return false;
                    return true;
                default:
                    if (string.IsNullOrWhiteSpace(value.ConnectionString))
                        return false;
                    return true;
            }
        }

        #endregion

        #endregion
    }
}